package br.sc.senai.control.managedbeans;

import java.io.IOException;
import java.util.Date;
import java.util.List;

import javax.faces.bean.ManagedBean;
import javax.faces.bean.RequestScoped;
import javax.faces.bean.SessionScoped;
import javax.faces.context.FacesContext;

import br.sc.senai.model.dao.Persist;
import br.sc.senai.model.entity.Automovel;
import br.sc.senai.model.exception.DAOExcption;

/**
 * 
 * @author Dhyego
 * @14/06/2013
 * @locadora-de-carros
 * @ClienteMB.java
 * @param <Automovel>
 */

@ManagedBean(name = "AutomovelMB")
@SessionScoped
public class AutomovelMB{

	
	private Persist<Automovel> daoAutomovel = new Persist<Automovel>();
	private Automovel automovel = new Automovel();
	private Automovel selectAutomovel;
	
	public Automovel getSelectAutomovel() {
		return selectAutomovel;
	}



	public void setSelectAutomovel(Automovel selectAutomovel) {
		this.selectAutomovel = selectAutomovel;
	}



	public void insert() {

				
		try {
			if (automovel.getId() != null && automovel.getId() > 0){
				daoAutomovel.update(automovel);
			} else {
				daoAutomovel.add(automovel);
			}
			automovel = new Automovel();
		} catch (DAOExcption e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
	}
	
	
	
	//Retorna um objeto pelo ID
	public Automovel getById(int id){
		
		Automovel aut = new Automovel();
		aut.setId(id);
		
		try {
			aut = daoAutomovel.getById(aut);
		} catch (DAOExcption e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		return aut;
		
	}
	
	//Retorna uma lista com todos Objetos
	public List<Automovel> loadAll() {

		List<Automovel> retorno = null;

		try {
			retorno = daoAutomovel.loadAll(Automovel.class);
		} catch (DAOExcption e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		
		return retorno;
	}
	
	
	//Deleta um objeto passando o objeto inteiro.
	public boolean delete(){
		
		boolean ok = false;
		
		try {
			if (selectAutomovel != null)
				daoAutomovel.delete(selectAutomovel);
			selectAutomovel = null;
		} catch (DAOExcption e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		
		return ok;
	}

	public void redirect(Boolean pIsCadastrarNovo){
		automovel = new Automovel();
		if (!pIsCadastrarNovo){
			try {
				//daoCliente.loadBySql("From Locacao l order by dtCadastro", arg1)
				if (selectAutomovel != null)
					automovel  = daoAutomovel.getById(selectAutomovel);
				else return;
						
			} catch (DAOExcption e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
		}
		
		try {
			FacesContext.getCurrentInstance().getExternalContext().redirect("cadastra-automovele.xhtml");
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
	}
	
	
	public Automovel getAutomovel() {
		return automovel;
	}

	public void setAutomovel(Automovel automovel) {
		this.automovel = automovel;
	}

}
